package com.cet.recon.step;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.Date;

@Component
@StepScope
public class LoadCoreTxnTasklet implements Tasklet {
  private final JdbcTemplate jdbc;
  public LoadCoreTxnTasklet(@Qualifier("bizJdbcTemplate") JdbcTemplate jdbc) { this.jdbc = jdbc; }

  @Value("#{jobParameters['run.date']}") String runDate;

  @Override
  public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
    jdbc.update("TRUNCATE TABLE stg_core_txn");
    jdbc.update("INSERT INTO stg_core_txn (tx_date,account_no,order_no,amount,currency,counterpart,tx_type,raw_source) " +
        "SELECT tx_date,account_no,order_no,amount,currency,counterpart,tx_type,'core' FROM core_txn WHERE tx_date=?", Date.valueOf(runDate));
    return RepeatStatus.FINISHED;
  }
}
